<template>
  <div class="container">
    <van-nav-bar
      fixed
      title="注册账号"
      @click-left="$router.back()"
      left-arrow
    />

    <div class="logo-txt">欢迎注册</div>
    <van-form @submit="onSubmit" label-width="70px">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="请输入手机号"
        left-icon="manager"
        :rules="[
          { required: true, message: '请填写用户名' },
          {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            message: '请输入正确手机号',
          },
        ]"
      />
      <van-field
        v-model="password1"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        left-icon="lock"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,12}$/, message: '请输入6-12位数字,字母,下划线' },
        ]"
      />
      <van-field
        v-model="password2"
        type="password"
        name="密码"
        label="确认密码"
        placeholder="确认密码"
        left-icon="lock"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,12}$/, message: '请输入6-12位数字,字母,下划线' },
          { validator, message: '两次密码不一致' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >立即注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// import {setToken} from '@/utils/storage'
import { register } from "@/api/register";
export default {
  data() {
    return {
      username: "",
      password1: "",
      password2: "",
    };
  },
  methods: {
    async onSubmit() {
      // console.log('submit', values)
      const res = await register({
        mobile: this.username,
        password: this.password1,
      });
      console.log(res);
			if(res.data.errno === 1000){
				this.$toast(res.data.errmsg);
			}else if(res.data.errno === 0){
				this.$toast(res.data.errmsg)
				// setToken(res.data.token)
				this.$router.push('/login')
			}
    },
    validator() {
      return this.password2 === this.password1;
    },
  },
};
</script>

<style scoped>
.logo-txt {
  text-align: center;
  font-size: 60px;
  margin: 60px auto;
  color: #1989fa;
}
</style>
